package com.hk.huakangcompany.service.impl;

import com.hk.huakangcompany.mapper.NewsTypeMapper;
import com.hk.huakangcompany.model.NewsType;
import com.hk.huakangcompany.utils.DateUtils;
import com.hk.huakangcompany.service.NewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zhe
 * @Date: 2021/6/16 10:34
 * @Statement: 新闻类型Service层接口实现类
 */
@Service
public class NewsTypeServiceImpl implements NewsTypeService {

    @Autowired
    NewsTypeMapper iNewsTypeMapper;

    @Override
    public List<NewsType> findAll(Integer page, Integer limit, NewsType t) {
        /*Page<Object> page1 = PageHelper.startPage(page, limit);
        page1.pageSizeZero(true);*/
        List<NewsType> newsTypeList = iNewsTypeMapper.findAll(t);
//        处理新闻类型
        if (newsTypeList.isEmpty()) {
            System.out.println("暂时没有新闻类型！");
            newsTypeList = new ArrayList<>();
            return newsTypeList;
        }
//        存储根节点新闻类型菜单
        ArrayList<NewsType> newsTypeArrayList = new ArrayList<>();
//        0:根节点
        for (NewsType newsType : newsTypeList) {
            if (newsType.getParentId() == 0) {
                newsTypeArrayList.add(newsType);
            }
        }
//        查询子节点
        for (NewsType newsType : newsTypeArrayList) {
            //        存储子级类型菜单
            List<NewsType> newsTypeList2 = getChild(newsType.getId(),newsTypeList);
            newsType.setNewsTypeList(newsTypeList2);
        }
        return newsTypeArrayList;
    }

    /**
     * 递归查询子节点
     * @param id 父级Id
     * @param newsTypeList
     * @return
     */
    public List<NewsType> getChild(int id,List<NewsType> newsTypeList) {
        ArrayList<NewsType> childList = new ArrayList<>();
        //      匹配父节点
        for (NewsType newsType : newsTypeList) {
//            匹配成功
            if (id == newsType.getParentId()) {
                childList.add(newsType);
            }
        }

        for (NewsType newsType : childList) {
            List<NewsType> typeList = getChild(newsType.getId(), newsTypeList);
            newsType.setNewsTypeList(typeList);
        }
//        当前节点没有子节点，退出
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

    @Override
    public int count(Integer page, Integer limit, NewsType t) {
//        PageHelper.startPage(page,limit);
        int count = iNewsTypeMapper.count(t);
        return count;
    }

    @Override
    public NewsType findById(Integer id) {
        NewsType newsType = iNewsTypeMapper.findById(id);
        return newsType;
    }

    /**
     * 递归查找当前Id对应的所属所有子节点ID
     * @param ids
     * @return
     */
    @Override
    public List<NewsType> findByParentId(List<Integer> ids) {
//         用来存储查询的值
        List<NewsType> newsTypes = new ArrayList<>();
        List<NewsType> newsTypeList = iNewsTypeMapper.findByParentId(ids);
        List<Integer> idList = newsTypeList.stream().map(NewsType::getId).collect(Collectors.toList());
//        递归匹配 2参数为匹配 1参数主要为赋值
        List<NewsType> newsTypeList1 = getfindByParentId(newsTypes,newsTypeList,idList);
        return newsTypes;
    }

    /**
     * 递归查询所属子节点
     * @return
     */
    public List<NewsType> getfindByParentId(List<NewsType> newsTypeList,List<NewsType> typeList,List<Integer> ids) {
//        判断不为空 赋值
        if (!typeList.isEmpty()) {
            for (NewsType newsType : typeList) {
                newsTypeList.add(newsType);
            }
            List<NewsType> newsTypeList1 = iNewsTypeMapper.findByParentId(ids);
            List<Integer> idList = newsTypeList1.stream().map(NewsType::getId).collect(Collectors.toList());
            getfindByParentId(newsTypeList,newsTypeList1,idList);
        }

        /**
         * 退出
         */
        if (typeList.isEmpty()) {
            return new ArrayList<NewsType>();
        }
        return newsTypeList;

    }

    @Override
    public int save(NewsType t) {
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtCreate(dateTimeString);
        t.setGmtModified(dateTimeString);
        int save = iNewsTypeMapper.save(t);
        if (save == 1) {
            System.out.println("新闻类型添加成功！");
        }
        return save;
    }

    @Override
    public int update(NewsType t) {
        //        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtModified(dateTimeString);
        int update = iNewsTypeMapper.update(t);
        if (update == 1) {
            System.out.println("新闻类型修改成功");
        }
        return update;
    }

    /**
     * 修改排序
     * @param t
     * @return
     */
    @Override
    public int updateParentId(NewsType t) {
        int updateParentId = 0;
//        判断修改序号是否与自身Id匹配或是否为负数
        if (t.getParentId() == t.getId() || t.getParentId() < 0) {
            return updateParentId;
        }
//       获取所有新闻类型
        List<NewsType> newsTypeList = iNewsTypeMapper.findAll(t);
        List<Integer> ids = newsTypeList.stream().map(NewsType::getId).collect(Collectors.toList());
//        判断所修改的序号是否存在
        for (Integer id : ids) {
            if (t.getParentId() == id) {
                // 修改日期时间
                String dateTimeString = DateUtils.dateTimeString(new Date());
                t.setGmtModified(dateTimeString);
                updateParentId = iNewsTypeMapper.updateParentId(t);
                if (updateParentId == 1) {
                    System.out.println("排序修改成功");
                }
                break;
            }
        }
        return updateParentId;

    }

    @Override
    public int delete(Integer id) {
        int delete = iNewsTypeMapper.delete(id);
        if (delete == 1) {
            System.out.println("新闻删除成功");
        }
        return delete;
    }

    @Override
    public int deleteIds(List<Integer> ids) {
        int delete = iNewsTypeMapper.deleteIds(ids);
        if (delete > 1) {
            System.out.println("多级新闻删除成功");
        }
        return delete;
    }

}
